<?php
/**
 * ImageServ.php
 *
 * @copyright   2023 thingsworld.com.cn - All Rights Reserved
 * @link        http://thingsworld.com.cn/
 * @author      stiffer.chen <chenl@thingsworld.com.cn>
 * @created     2023-02-16 03:20:40
 * @modified    2023-02-16 03:20:40
 *
 */

namespace App\Services;

use Illuminate\Support\Str;

class ImageServ
{
    private $image;
    private static $instance;

    public function __construct($image)
    {
        $this->image = $image;
    }

    public static function getInstance(string $image)
    {
        if (!(self::$instance instanceof self)) {
            self::$instance = new self($image);
        }
        return self::$instance;
    }

    public function getUrl()
    {
        $baseUrl = trim(env('APP_URL'), '/') . '/';
        if (!$this->image) {
            return $baseUrl . 'images/placeholder.png';
        }
        if (Str::startsWith($this->image, 'http://') || Str::startsWith($this->image, 'https://')) {
            return $this->image;
        }
        if (!is_file(public_path() . '/' . $this->image)) {
            return $baseUrl . 'images/placeholder.png';
        }
        return $baseUrl . $this->image;
    }
}